/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tubosmonterrey.cotizador.sql;

import com.tubosmonterrey.cotizador.sql.exceptions.NonexistentEntityException;
import com.tubosmonterrey.cotizador.sql.exceptions.PreexistingEntityException;
import com.tubosmonterrey.cotizador.tb.Tbiva;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.tubosmonterrey.cotizador.tb.Tbcotizacion;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author rsoberanis
 */
public class TbivaJpaController implements Serializable {

    public TbivaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Tbiva tbiva) throws PreexistingEntityException, Exception {
        if (tbiva.getTbcotizacionCollection() == null) {
            tbiva.setTbcotizacionCollection(new ArrayList<Tbcotizacion>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Tbcotizacion> attachedTbcotizacionCollection = new ArrayList<Tbcotizacion>();
            for (Tbcotizacion tbcotizacionCollectionTbcotizacionToAttach : tbiva.getTbcotizacionCollection()) {
                tbcotizacionCollectionTbcotizacionToAttach = em.getReference(tbcotizacionCollectionTbcotizacionToAttach.getClass(), tbcotizacionCollectionTbcotizacionToAttach.getId());
                attachedTbcotizacionCollection.add(tbcotizacionCollectionTbcotizacionToAttach);
            }
            tbiva.setTbcotizacionCollection(attachedTbcotizacionCollection);
            em.persist(tbiva);
            for (Tbcotizacion tbcotizacionCollectionTbcotizacion : tbiva.getTbcotizacionCollection()) {
                Tbiva oldIvaOfTbcotizacionCollectionTbcotizacion = tbcotizacionCollectionTbcotizacion.getIva();
                tbcotizacionCollectionTbcotizacion.setIva(tbiva);
                tbcotizacionCollectionTbcotizacion = em.merge(tbcotizacionCollectionTbcotizacion);
                if (oldIvaOfTbcotizacionCollectionTbcotizacion != null) {
                    oldIvaOfTbcotizacionCollectionTbcotizacion.getTbcotizacionCollection().remove(tbcotizacionCollectionTbcotizacion);
                    oldIvaOfTbcotizacionCollectionTbcotizacion = em.merge(oldIvaOfTbcotizacionCollectionTbcotizacion);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findTbiva(tbiva.getId()) != null) {
                throw new PreexistingEntityException("Tbiva " + tbiva + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Tbiva tbiva) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tbiva persistentTbiva = em.find(Tbiva.class, tbiva.getId());
            Collection<Tbcotizacion> tbcotizacionCollectionOld = persistentTbiva.getTbcotizacionCollection();
            Collection<Tbcotizacion> tbcotizacionCollectionNew = tbiva.getTbcotizacionCollection();
            Collection<Tbcotizacion> attachedTbcotizacionCollectionNew = new ArrayList<Tbcotizacion>();
            for (Tbcotizacion tbcotizacionCollectionNewTbcotizacionToAttach : tbcotizacionCollectionNew) {
                tbcotizacionCollectionNewTbcotizacionToAttach = em.getReference(tbcotizacionCollectionNewTbcotizacionToAttach.getClass(), tbcotizacionCollectionNewTbcotizacionToAttach.getId());
                attachedTbcotizacionCollectionNew.add(tbcotizacionCollectionNewTbcotizacionToAttach);
            }
            tbcotizacionCollectionNew = attachedTbcotizacionCollectionNew;
            tbiva.setTbcotizacionCollection(tbcotizacionCollectionNew);
            tbiva = em.merge(tbiva);
            for (Tbcotizacion tbcotizacionCollectionOldTbcotizacion : tbcotizacionCollectionOld) {
                if (!tbcotizacionCollectionNew.contains(tbcotizacionCollectionOldTbcotizacion)) {
                    tbcotizacionCollectionOldTbcotizacion.setIva(null);
                    tbcotizacionCollectionOldTbcotizacion = em.merge(tbcotizacionCollectionOldTbcotizacion);
                }
            }
            for (Tbcotizacion tbcotizacionCollectionNewTbcotizacion : tbcotizacionCollectionNew) {
                if (!tbcotizacionCollectionOld.contains(tbcotizacionCollectionNewTbcotizacion)) {
                    Tbiva oldIvaOfTbcotizacionCollectionNewTbcotizacion = tbcotizacionCollectionNewTbcotizacion.getIva();
                    tbcotizacionCollectionNewTbcotizacion.setIva(tbiva);
                    tbcotizacionCollectionNewTbcotizacion = em.merge(tbcotizacionCollectionNewTbcotizacion);
                    if (oldIvaOfTbcotizacionCollectionNewTbcotizacion != null && !oldIvaOfTbcotizacionCollectionNewTbcotizacion.equals(tbiva)) {
                        oldIvaOfTbcotizacionCollectionNewTbcotizacion.getTbcotizacionCollection().remove(tbcotizacionCollectionNewTbcotizacion);
                        oldIvaOfTbcotizacionCollectionNewTbcotizacion = em.merge(oldIvaOfTbcotizacionCollectionNewTbcotizacion);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = tbiva.getId();
                if (findTbiva(id) == null) {
                    throw new NonexistentEntityException("The tbiva with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tbiva tbiva;
            try {
                tbiva = em.getReference(Tbiva.class, id);
                tbiva.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tbiva with id " + id + " no longer exists.", enfe);
            }
            Collection<Tbcotizacion> tbcotizacionCollection = tbiva.getTbcotizacionCollection();
            for (Tbcotizacion tbcotizacionCollectionTbcotizacion : tbcotizacionCollection) {
                tbcotizacionCollectionTbcotizacion.setIva(null);
                tbcotizacionCollectionTbcotizacion = em.merge(tbcotizacionCollectionTbcotizacion);
            }
            em.remove(tbiva);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Tbiva> findTbivaEntities() {
        return findTbivaEntities(true, -1, -1);
    }

    public List<Tbiva> findTbivaEntities(int maxResults, int firstResult) {
        return findTbivaEntities(false, maxResults, firstResult);
    }

    private List<Tbiva> findTbivaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Tbiva.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Tbiva findTbiva(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Tbiva.class, id);
        } finally {
            em.close();
        }
    }

    public int getTbivaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Tbiva> rt = cq.from(Tbiva.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
